﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
using XinYue.Permission.BLL;
using XinYue.Permission.Mode;

namespace XinYue.Permission.Web.Controllers
{
    public class RoleController : Controller
    {
        /// <summary>
        /// 获取角色列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public JsonResult GetRoleList(int page, int limit)
        {
            PageListresult<RoleInfo> pageListResult = new PageListresult<RoleInfo>();
            RoleService roleService = new RoleService();
            List<RoleInfo> roleUserQueries = roleService.GetRoleList();
            pageListResult.data = roleUserQueries.Skip((page - 1) * limit).Take(limit).ToList();
            pageListResult.msg = string.Empty;
            pageListResult.code = 0;
            pageListResult.count = roleUserQueries.Count;
            return Json(pageListResult, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 删除权限
        /// </summary>
        /// <param name="addRoleInfo"></param>
        /// <returns></returns>
        public JsonResult DelRole(int id)
        {
            Operate operateResult = new Operate();
            //创建业务层对象
            BaseService<AdminUserRole> service = new BaseService<AdminUserRole>();
            BaseService<RoleFunctionPermission> roleFunctionService = new BaseService<RoleFunctionPermission>();
            BaseService<ModuleRole> moduleRoleService = new BaseService<ModuleRole>();
            //定义查询拉姆达表达式
            Expression<Func<AdminUserRole, bool>> adminUserRoleLambda = a => a.RoleId == id;
            Expression<Func<RoleFunctionPermission, bool>> roleFunctionLambda = a => a.RoleId == id;
            Expression<Func<ModuleRole, bool>> moduleRoleLambda = a => a.RoleId == id;
            ModuleRole moduleRole = moduleRoleService.Query(moduleRoleLambda);
            List<RoleFunctionPermission> roleFunctionList = roleFunctionService.QueryList(roleFunctionLambda);
            List<AdminUserRole> roleList = service.QueryList(adminUserRoleLambda);
            //判断用户角色关联表是否有记录
            if (roleList.Count != 0)
            {
                operateResult.Success = false;
                return Json(operateResult);
            }
            //调用自定义删除方法增加
            RoleService roleService = new RoleService();
            Role role = new Role();
            role.Id = id;
            operateResult.Success = roleService.Delete(role, roleFunctionList, moduleRole);
            return Json(operateResult);
        }
        /// <summary>
        /// 添加权限
        /// </summary>
        /// <param name="addRoleInfo"></param>
        /// <returns></returns>
        public JsonResult AddRole(AddRoleInfo addRoleInfo)
        {
            Operate operateResult = new Operate();
            //创建业务层对象
            RoleService roleService = new RoleService();
            //定义查询拉姆达表达式
            Expression<Func<Role, bool>> roleLambda = a => a.Id != 0;
            List<Role> roleList = roleService.QueryList(roleLambda);
            for (int i = 0; i < roleList.Count; i++)
            {
                if (addRoleInfo.role.Name == roleList[i].Name)
                {
                    operateResult.Success = false;
                    return Json(operateResult);
                }
            }
            //获取组Id
            int moduleId = addRoleInfo.ModuleId;
            ///获取Role对象
            Role role = addRoleInfo.role;
            //属性赋值
            role.IsEnable = true;
            role.CreateTime = DateTime.Now;
            //获取角色菜单功能关联表集合
            List<RoleFunctionPermission> roleFunctionList = addRoleInfo.roleFunctionList;
            if (roleFunctionList == null)
            {
                operateResult.Success = roleService.Add(role, moduleId);
                return Json(operateResult);
            }
            //调用自定义Add方法增加
            operateResult.Success = roleService.Add(role, roleFunctionList, moduleId);
            return Json(operateResult);
        }
        /// <summary>
        /// 修改权限
        /// </summary>
        /// <param name="addRoleInfo"></param>
        /// <returns></returns>
        public JsonResult UpdateRole(AddRoleInfo addRoleInfo, int id)
        {
            Operate operateResult = new Operate();
            //创建业务层对象
            RoleService roleService = new RoleService();
            BaseService<ModuleRole> moduleRoleService = new BaseService<ModuleRole>();
            BaseService<RoleFunctionPermission> roleFunctionService = new BaseService<RoleFunctionPermission>();
            //定义查询拉姆达表达式
            Expression<Func<Role, bool>> roleLambda = a => a.Id != 0;
            Expression<Func<Role, bool>> rolesLambda = a => a.Id == id;
            Expression<Func<RoleFunctionPermission, bool>> roleFunctionLambda = a => a.RoleId == id;
            Expression<Func<ModuleRole, bool>> moduleRoleLambda = a => a.RoleId == id;
            //接收查询结果
            List<Role> roleList = roleService.QueryList(roleLambda);
            Role role1 = roleService.Query(rolesLambda);
            List<RoleFunctionPermission> oleRoleFunctionList = roleFunctionService.QueryList(roleFunctionLambda);
            ModuleRole moduleRole = moduleRoleService.Query(moduleRoleLambda);
            //判断名字是否存在一致
            for (int i = 0; i < roleList.Count; i++)
            {
                if (addRoleInfo.role.Name == roleList[i].Name && id != roleList[i].Id)
                {
                    operateResult.Success = false;
                    return Json(operateResult);
                }
            }
            //获取组Id
            moduleRole.ModuleId = addRoleInfo.ModuleId;
            ///获取Role对象
            Role role = addRoleInfo.role;
            //属性赋值
            role.Id = id;
            role.CreateTime = role1.CreateTime;
            role.IsEnable = role1.IsEnable;
            //获取角色菜单功能关联表集合
            List<RoleFunctionPermission> newRoleFunctionList = addRoleInfo.roleFunctionList;
            if (newRoleFunctionList == null)
            {
                operateResult.Success = roleService.Update(role, oleRoleFunctionList, moduleRole);
                return Json(operateResult);
            }
            //调用自定义Add方法增加
            operateResult.Success = roleService.Update(role, newRoleFunctionList, oleRoleFunctionList, moduleRole);
            return Json(operateResult);
        }
    }
}